Shader "Custom/CharacterLightMask"
{
    Properties
    {
        _MainTex ("Base Texture", 2D) = "white" {}
        _PlayerPos ("Player Position", Vector) = (0,0,0,0)
        _VisibleRadius ("Visible Radius", Range(0, 10)) = 5
    }

    SubShader
    {
        Tags
        {
            "Queue"="Transparent" "RenderType"="Transparent"
        }
        Blend SrcAlpha OneMinusSrcAlpha

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 pos : SV_POSITION;
                float3 worldPos : TEXCOORD1;
            };

            sampler2D _MainTex;
            float3 _PlayerPos;
            float _VisibleRadius;

            v2f vert(appdata v)
            {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
                return o;
            }

            fixed4 frag(v2f i) : SV_Target
            {
                // 计算到玩家的距离
                float dist = distance(_PlayerPos, i.worldPos);

                // 计算可见度
                // float visibility = smoothstep(
                //     _VisibleRadius, 
                //     _VisibleRadius * (1 - _EdgeSmoothness), 
                //     dist
                // );

                // 混合颜色
                // fixed4 col = tex2D(_MainTex, i.uv);
                // return fixed4(col.rgb * visibility, visibility);

                if (dist < _VisibleRadius)
                {
                    float alpha = dist / _VisibleRadius;
                    return fixed4(0, 0, 0, alpha);
                }
                else
                {
                    return fixed4(0, 0, 0, 1);
                }
            }
            ENDCG
        }
    }
}